package com.crlui.control
{
	import com.crlui.control.skins.IButtonSkin;
	import com.crlui.control.skins.ISkin;
	
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 
	 * @author crl
	 * 
	 */	
	public class Button extends UIComponent
	{
		private var skin:IButtonSkin;
		protected var _enabled:Boolean=true;
		protected var _selected:Boolean=false;
		protected var _toggle:Boolean=false;
		
		protected var _label:String;
		
		public function Button(skin:IButtonSkin)
		{
			super();
			
			setSkin(skin);
			
			this.addEventListener(MouseEvent.CLICK,mouseHandler);
		}
		
		
		override public function setSkin(value:ISkin):void{
			skin=value as IButtonSkin;
			super.setSkin(value);
		}
		
		public function set label(value:String):void{
			
			if(_label==value)return;
			
			_label=value;
			skin.label=value;
		}
		
		
		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			if(_enabled != value){
				_enabled = value;
				skin.enabled=value;
			}
		}

		public function get toggle():Boolean
		{
			return _toggle;
		}

		public function set toggle(value:Boolean):void
		{
			_toggle = value;
			if(_toggle==false)selected=false;
		}

		public function set selected(value:Boolean):void{
			if(_selected !=value && toggle){
				_selected=value;
				skin.selected=value;
				this.dispatchEvent(new Event(Event.SELECT));
			}
		}
		
		public function get selected():Boolean{
			return _selected;
		}
		
		
		protected function mouseHandler(event:MouseEvent):void{
			if(_enabled==false)return;
			selected=!selected;
		}
	}
}